package com.victor.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.Express;
import com.victor.entity.JingqubusinessOrder;
import com.victor.service.OrderService;
import com.victor.utils.Msg;

import net.bytebuddy.asm.Advice.Return;

/**
 * 	电商订单管理
 * @author qinyuquan
 *
 */
@RestController
@RequestMapping("/order")
public class OrderController {
	
	@Autowired
	private LogController logcontroller;
	
	@Autowired
	private OrderService orderService;
	
	/**
	 * 根据传入order字段，查询order列表
	 * id 订单id
	 * o_name 订单名称
	 * uid 下单用户id
	 * unickname 下单用户昵称
	 * state 订单状态（1新订单2待出库3待收货4已收货5待评价6已评价7待退款8已退款）
	 */
	@RequestMapping("/selectOderList")
	public Msg selectOderList(JingqubusinessOrder order) {
		if (order == null) {
			return Msg.fail("参数错误");
		}
		List<JingqubusinessOrder> orders = orderService.selectOderList(order);
		return Msg.success().add("orders", orders);
	}
	
	/**
	 * 	search搜索order列表  
	 * 	如果search值为"",则查询所有列表
	 */
	@RequestMapping("/search")
	public Msg selectOderListBySearch(String search) {
		if (search==null) {
			return Msg.fail("参数错误");
		}
		List<JingqubusinessOrder> orderList = orderService.selectOderListBySearch(search);
		return Msg.success().add("orderList", orderList);
	}
	

	/**
	 * 	批量删除订单
	 */
	@RequestMapping("/delByIds")
	public Msg delByIds(int[] ids,String adminid) {
		if (ids==null || ids.length==0 || adminid == null) {
			return Msg.fail("参数错误");
		}
		//删除订单表
		boolean b = orderService.batchDelete(ids);
		if (!b) {
			return Msg.fail("删除订单失败");
		}
		logcontroller.addLog(adminid, "删除了用户订单的内容");
		//删除关联收件人信息
		boolean address = orderService.batchDeleteAddress(ids);
		if (!address) {
			return Msg.fail("删除关联收件人信息失败");
		}
		//删除关联快递信息表
		boolean express = orderService.batchDeleteExpress(ids);
		if (!express) {
			return Msg.fail("删除订单快递信息失败");
		}
		//删除购买的商品
		boolean shops = orderService.batchDeleteShops(ids);
		if (!shops) {
			return Msg.fail("删除购买的商品失败");
		}
		return Msg.judge(b);
	}
	

	/**
	 *	 订单详细列表
	 */
	@RequestMapping("/selectDeatil")
	public Msg selectDeatil(Integer id) {
		if (id==null || id.equals("")) {
			return Msg.fail("参数错误");
		}
		JingqubusinessOrder order = orderService.selectDeatil(id);
		return Msg.success().add("order", order);
	}
	
	/**
	 * 	订单操作，接单
	 *	@param state字段中： 1新订单，2待出库，3待收货，4已收货，5待评价，6已评价，7待退款，8已退款
	 *	@param i 订单id
	 *	通过id，传什么字段，就修改什么字段
	 */
	@RequestMapping("/receivingOrder")
	public Msg receivingOrderById(Express express,Integer i, String state, String adminid) {
		if (i==null ||i.equals("")||state==null || state.equals("")|| adminid==null ||adminid.equals("") ||express==null || express.getUserid()==null ||express.getOrderid()==null ||express.getExpressname()==null ||express.getExpressname().equals("")||express.getExpressnumber()==null ||express.getExpressnumber().equals("")) {
			return Msg.fail("参数错误！");
		}
		boolean b = orderService.receivingOrder(i,state);
		boolean c = orderService.addExpress(express);
		if (b==true && c==true) {
			logcontroller.addLog(adminid, "接单成功");
			return Msg.judge(b);
		}
		logcontroller.addLog(adminid, "接单失败");
		return Msg.fail("接单失败");
	}
}
